package ethereum

import (
	"encoding/json"
	"testing"

	"github.com/hyperledger/burrow/rpc/web3/ethclient"
	"github.com/hyperledger/burrow/vent/chain"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestConsumeBlocks(t *testing.T) {
	var logs []*ethclient.EthLog
	err := json.Unmarshal([]byte(ethLogsJSON), &logs)
	require.NoError(t, err)
	var blocks []chain.Block
	lastBlock, err := consumeBlocksFromLogs(nil, logs, func(block chain.Block) error {
		blocks = append(blocks, block)
		return nil
	})
	require.NoError(t, err)
	assert.Equal(t, uint64(24), lastBlock.GetHeight())
	assert.Len(t, blocks, 3)
	txs0 := blocks[0].GetTxs()
	assert.Len(t, txs0, 2)
	assert.Len(t, txs0[0].GetEvents(), 1)
	assert.Len(t, txs0[1].GetEvents(), 2)
	assert.Len(t, blocks[1].GetTxs(), 1)
	assert.Len(t, blocks[2].GetTxs(), 1)
}

func TestConsumer_ConsumeInBatches(t *testing.T) {
	// TODO
}

// From running TestGetEthLogs over in ethclient tests against truffle in vent/test/eth (and subsequently fiddled with)
// language=JSON
const ethLogsJSON = `[
  {
    "topics": [
      "0x2d989eca8871e173291c8e287f34adebef09917027f9e904c22ce459a2cff0ca",
      "0x446f776e73696521000000000000000000000000000000000000000000000000",
      "0x0000000000000000000000000000000000000000000000000000000000000066",
      "0x33027547537d35728a741470df1ccf65de10b454ca0def7c5c20b257b7b8d161"
    ],
    "transactionHash": "0x22d1cbba63e3577a5a35b02945289711e1e21a8e3d69dd2f3b05fd4619eb6b20",
    "address": "0xa1e378f122fec6aa8c841397042e21bc19368768",
    "blockHash": "0x434520f95c0ca8ad4b1853b6aeb748593b4e8603d923199c44c753133fa66b7b",
    "blockNumber": "0x9",
    "data": "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000051446f6e617564616d7066736368696666666168727473656c656b7472697a6974c3a474656e686175707462657472696562737765726b626175756e7465726265616d74656e676573656c6c736368616674000000000000000000000000000000",
    "logIndex": "0x0",
    "transactionIndex": "0x0"
  },
  {
    "topics": [
      "0x2d989eca8871e173291c8e287f34adebef09917027f9e904c22ce459a2cff0ca",
      "0x446f776e73696521000000000000000000000000000000000000000000000000",
      "0x0000000000000000000000000000000000000000000000000000000000000066",
      "0x33027547537d35728a741470df1ccf65de10b454ca0def7c5c20b257b7b8d161"
    ],
    "transactionHash": "0xf1869bff3273e8a31076be06754c8d1ae2bbd38d9faf32cfeb411750878c7ee5",
    "address": "0xf73aaa468496a87675d27638878a1600b0db3c71",
    "blockHash": "0x9fe9d2eb6b309330c48a77793dbb98bf01faf5d3aea8e0fde60c7d548330ae74",
    "blockNumber": "0x9",
    "data": "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000051446f6e617564616d7066736368696666666168727473656c656b7472697a6974c3a474656e686175707462657472696562737765726b626175756e7465726265616d74656e676573656c6c736368616674000000000000000000000000000000",
    "logIndex": "0x1",
    "transactionIndex": "0x0"
  },
  {
    "topics": [
      "0x2d989eca8871e173291c8e287f34adebef09917027f9e904c22ce459a2cff0ca",
      "0x446f776e73696521000000000000000000000000000000000000000000000000",
      "0x0000000000000000000000000000000000000000000000000000000000000066",
      "0x33027547537d35728a741470df1ccf65de10b454ca0def7c5c20b257b7b8d161"
    ],
    "transactionHash": "0xf1869bff3273e8a31076be06754c8d1ae2bbd38d9faf32cfeb411750878c7ee5",
    "address": "0xf73aaa468496a87675d27638878a1600b0db3c71",
    "blockHash": "0x9fe9d2eb6b309330c48a77793dbb98bf01faf5d3aea8e0fde60c7d548330ae74",
    "blockNumber": "0x9",
    "data": "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000051446f6e617564616d7066736368696666666168727473656c656b7472697a6974c3a474656e686175707462657472696562737765726b626175756e7465726265616d74656e676573656c6c736368616674000000000000000000000000000000",
    "logIndex": "0x4",
    "transactionIndex": "0x0"
  },
  {
    "topics": [
      "0x2d989eca8871e173291c8e287f34adebef09917027f9e904c22ce459a2cff0ca",
      "0x446f776e73696521000000000000000000000000000000000000000000000000",
      "0x0000000000000000000000000000000000000000000000000000000000000066",
      "0x33027547537d35728a741470df1ccf65de10b454ca0def7c5c20b257b7b8d161"
    ],
    "transactionHash": "0x919b7ca6cd104bbb9cef2a5199ba8515968494474874894e426227766759d442",
    "address": "0xc0eec83d13999827902ad7f2ebba38e4ce457c88",
    "blockHash": "0x099fa767303fd80ae83a82d383a87c92662f5e59cf1ed87b55cf1b9c4aea9f6b",
    "blockNumber": "0x13",
    "data": "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000051446f6e617564616d7066736368696666666168727473656c656b7472697a6974c3a474656e686175707462657472696562737765726b626175756e7465726265616d74656e676573656c6c736368616674000000000000000000000000000000",
    "logIndex": "0x0",
    "transactionIndex": "0x0"
  },
  {
    "topics": [
      "0x2d989eca8871e173291c8e287f34adebef09917027f9e904c22ce459a2cff0ca",
      "0x446f776e73696521000000000000000000000000000000000000000000000000",
      "0x0000000000000000000000000000000000000000000000000000000000000066",
      "0x33027547537d35728a741470df1ccf65de10b454ca0def7c5c20b257b7b8d161"
    ],
    "transactionHash": "0x6979538ef67d056b72f62d711dc63de0b06c8d4c31e1788bed973d07719f7ee5",
    "address": "0xbe6cea7a6cc115ed2c0863dc2df6b883769b2ec5",
    "blockHash": "0x3f9a059783e476eea8f723179431498ad62c584c6ea02c4b3cb5b541cd3435b4",
    "blockNumber": "0x18",
    "data": "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000051446f6e617564616d7066736368696666666168727473656c656b7472697a6974c3a474656e686175707462657472696562737765726b626175756e7465726265616d74656e676573656c6c736368616674000000000000000000000000000000",
    "logIndex": "0x0",
    "transactionIndex": "0x0"
  }
]`
